package com.clps.rulesregulations.service.impl;

import com.clps.rulesregulations.dao.UserDao;
import com.clps.rulesregulations.domain.User;
import com.clps.rulesregulations.service.UserRoleService;
import com.clps.rulesregulations.service.UserService;
import com.clps.rulesregulations.util.CommonUtil;
import com.clps.rulesregulations.util.PasswordHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户逻辑处理
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Autowired
    private UserRoleService userRoleService;
    @Override
    public User findUserByUsername(String username) {
        return userDao.findUserByUsername(username);
    }

    @Override
    public User checkUser(String username,String id) {
        return userDao.checkUser(username,id);
    }

    @Override
    public PageInfo<User> findUsersByPage(Map<String, Object> condition) {
        Integer pageNum = 1;
        try {
            pageNum = (Integer) condition.get("pageNum");
            pageNum = pageNum==0?1:pageNum;
        }catch (Exception e){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,10);
        List<User> users = userDao.findUsersByPage(condition);
        return new PageInfo<>(users);
    }

    @Override
    public PageInfo<User> findUsersByList(Map<String, Object> condition) {
        Integer pageNum = 1;
        try {
            pageNum = (Integer) condition.get("pageNum");
            pageNum = pageNum==0?1:pageNum;
        }catch (Exception e){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,5);
        List<User> users = userDao.findUsersByList(condition);
        return new PageInfo<>(users);
    }

    @Override
    public List<User> listAll(Map<String, Object> condition) {
        List<User> users = userDao.findUsersByPage(condition);
        return users;
    }

    @Override
    public PageInfo<User> findUsersByDeptId(Map<String, Object> condition) {
        Integer pageNum = 1;
        try {
            pageNum = (Integer) condition.get("pageNum");
            pageNum = pageNum==0?1:pageNum;
        }catch (Exception e){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,10);
        List<User> users = userDao.findUsersByDeptId(condition);
        return new PageInfo<>(users);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public void delUser(Integer userId) {
        userDao.delUser(userId);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void addUser(User user) {

//        String uuid = CommonUtil.getUuid();
//        user.setId(uuid);
        List list = new ArrayList();
        list.add(2l);
        user.setCreateTime(new Date());
        user.setLastLoginTime(new Date());

        new PasswordHelper().encryptPassword(user);
        userDao.addUser(user);
        userRoleService.updateUserRole(user.getId(),list);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void updatePassword(User user) {

//        String uuid = CommonUtil.getUuid();
//        user.setId(uuid);
//        new PasswordHelper().encryptPassword(user);
        userDao.updatePassword(user);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void lockUser(Integer userId) {
        userDao.lockUser(userId);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void recoveryUser(Integer userId) {
        userDao.recoveryUser(userId);
    }

    @Override
    public User findUserByUserId(Integer userId) {
        return userDao.findUserByUserId(userId);
    }
}
